package com.yhy.wxzn.wcs.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yhy.wxzn.common.enums.ValidEnums;
import com.yhy.wxzn.common.enums.WcsDeviceTypeEnums;
import com.yhy.wxzn.wcs.device.plc.bean.FourCarPlc;
import com.yhy.wxzn.wcs.device.plc.bean.HoistPlc;
import com.yhy.wxzn.wcs.device.plc.siemens.SiemensPlcDriver;
import com.yhy.wxzn.wcs.device.plc.siemens.SiemensPlcServer;
import com.yhy.wxzn.wcs.device.plc.xj.XjPlcDriver;
import com.yhy.wxzn.wcs.device.plc.xj.XjPlcServer;
import com.yhy.wxzn.wcs.device.rgv.bean.Coord;
import com.yhy.wxzn.wcs.entity.DeviceNetworkInfo;
import com.yhy.wxzn.wcs.entity.WcsDeviceInfo;
import com.yhy.wxzn.wcs.mapper.WcsDeviceInfoMapper;
import com.yhy.wxzn.wcs.service.IWcsDeviceInfoService;
import com.yhy.wxzn.wcs.wcsWmsCommunication.bean.ReturnMsgDevice;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 设备信息
 * @Author: jeecg-boot
 * @Date:   2023-10-28
 * @Version: V1.0
 */
@Service
public class WcsDeviceInfoServiceImpl extends ServiceImpl<WcsDeviceInfoMapper, WcsDeviceInfo> implements IWcsDeviceInfoService {

    @Autowired
    private WcsDeviceInfoMapper deviceInfoMapper;


    @Override
    public List<WcsDeviceInfo> findByDeviceType(Integer deviceType) {
        LambdaQueryWrapper<WcsDeviceInfo> devQuery = new LambdaQueryWrapper<>();
        devQuery.eq(WcsDeviceInfo::getDeviceType,deviceType);
        devQuery.eq(WcsDeviceInfo::getStatus, ValidEnums.ENABLE.getValue());
        return this.list(devQuery);
    }

    @Override
    public List<WcsDeviceInfo> findAllByDeviceType(Integer deviceType) {
        LambdaQueryWrapper<WcsDeviceInfo> devQuery = new LambdaQueryWrapper<>();
        devQuery.eq(WcsDeviceInfo::getDeviceType,deviceType);
        return this.list(devQuery);
    }

    @Override
    public WcsDeviceInfo findByDeviceName(String deviceName) {
        LambdaQueryWrapper<WcsDeviceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WcsDeviceInfo::getDeviceName,deviceName);
        return getOne(queryWrapper);
    }

    @Override
    public Result<?> changeDeviceStatus(String coord,String bePort) {
//        LambdaQueryWrapper<WcsCoordConfig> coordQuery = new LambdaQueryWrapper<>();
//        coordQuery.eq(WcsCoordConfig::getCoord,coord);
//        List<WcsCoordConfig> configs = coordConfigService.list(coordQuery);
//        if(!CollectionUtils.isEmpty(configs)){
//            XjPlcDriver driver = XjPlcServer.plcDriverMap.get(configs.get(0).getPlcNo());
//            if("0".equals(bePort)){
//                TransmissionPlc transmissionPlc = driver.plcDataPacket.getTransmissionReadPlcMap().get(coord);
//                transmissionPlc.setAuto(transmissionPlc.isAuto()?false:true);
//            }else{
//                PortPlc portPlc = driver.plcDataPacket.getPortReadPlcMap().get(coord);
//                portPlc.setAuto(portPlc.isAuto()?false:true);
//            }
//        }
        return Result.OK();
    }

    @Override
    public Result<?> controlDeviceAll(String plcNo, String dealType) {
        XjPlcDriver driver = XjPlcServer.plcDriverMap.get(plcNo);
//        driver.plcDataPacket.getTransmissionReadPlcMap().entrySet().parallelStream().forEach(transmissionPlcEntry -> {
//            TransmissionPlc transmissionPlc = transmissionPlcEntry.getValue();
//            if("1".equals(dealType)){
//                transmissionPlc.setAuto(Boolean.TRUE);
//            }else{
//                transmissionPlc.setAuto(Boolean.FALSE);
//            }
//        });
//        driver.plcDataPacket.getPortReadPlcMap().entrySet().parallelStream().forEach(portPlcEntry -> {
//            PortPlc portPlc = portPlcEntry.getValue();
//            if("1".equals(dealType)){
//                portPlc.setAuto(Boolean.TRUE);
//            }else{
//                portPlc.setAuto(Boolean.FALSE);
//            }
//        });
        return Result.OK();
    }

    @Override
    public Result<?> devicePlcInfo(String deviceId, String deviceType) {
        if(WcsDeviceTypeEnums.FWV.getCode().equals(deviceType)){
            return genFourCarPlcInfo(deviceId);
        }else if(WcsDeviceTypeEnums.ELV.getCode().equals(deviceType)){
            return genHoistPlcInfo(deviceId);
        }
        return Result.OK(new ArrayList<>());
    }

    @Override
    public void refreshRgvLocation(Integer rgvId, Coord coord) {
        LambdaUpdateWrapper<WcsDeviceInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(WcsDeviceInfo::getId,rgvId)
                .set(WcsDeviceInfo::getLocation, coord.toString());

        baseMapper.update(null,updateWrapper);
    }

    @Override
    public void getReturnMsgDeviceByDeviceId(List<ReturnMsgDevice> devices, Integer deviceId) {
        WcsDeviceInfo deviceInfo = getById(deviceId);
        if(deviceInfo == null) return;


        if (WcsDeviceTypeEnums.FWV.getValue().equals(deviceInfo.getDeviceType())) {
            ReturnMsgDevice.buildRgvInfo(devices,deviceInfo);
        }else if (WcsDeviceTypeEnums.ELV.getValue().equals(deviceInfo.getDeviceType())) {
            ReturnMsgDevice.buildLifterInfo(devices,deviceInfo);
        }
    }

    @Override
    public void getReturnMsgDeviceByDeviceType(List<ReturnMsgDevice> devices, Integer deviceType) {
        List<WcsDeviceInfo> list = list();
        for (WcsDeviceInfo deviceInfo : list) {
            WcsDeviceTypeEnums wcsDeviceTypeEnums = ReturnMsgDevice.parseDeviceType(deviceType);
            if (wcsDeviceTypeEnums.getValue().equals(deviceInfo.getDeviceType())){
                if (WcsDeviceTypeEnums.FWV.getValue().equals(deviceInfo.getDeviceType())) {
                    ReturnMsgDevice.buildRgvInfo(devices,deviceInfo);
                }else if (WcsDeviceTypeEnums.ELV.getValue().equals(deviceInfo.getDeviceType())) {
                    ReturnMsgDevice.buildLifterInfo(devices,deviceInfo);
                }
            }
        }

    }

    @Override
    public List<DeviceNetworkInfo> findDeviceNetworkInfoByDeviceType(Integer value) {
        return deviceInfoMapper.findDeviceNetworkInfoByDeviceType(value);
    }

    private Result<?> genHoistPlcInfo(String deviceId) {
        if(!XjPlcServer.plcDriverMap.isEmpty()){
            List<HoistPlc> dataList = new ArrayList<>();
            XjPlcDriver xjPlcDriver = XjPlcServer.plcDriverMap.get(deviceId);
            HoistPlc hoistPlc = xjPlcDriver.getDriverData().getHoistPlcMap().get(deviceId);
            dataList.add(hoistPlc);
            return Result.OK(dataList);
        }
        if(!SiemensPlcServer.plcDriverMap.isEmpty()){
            List<HoistPlc> dataList = new ArrayList<>();
            SiemensPlcDriver siemensPlcDriver = SiemensPlcServer.plcDriverMap.get(deviceId);
            HoistPlc hoistPlc = siemensPlcDriver.getDriverData().getHoistPlcMap().get(deviceId);
            dataList.add(hoistPlc);
            return Result.OK(dataList);
        }
        return Result.OK(new ArrayList<>());
    }

    private Result<?> genFourCarPlcInfo(String deviceId) {
        if(!XjPlcServer.plcDriverMap.isEmpty()){
            List<FourCarPlc> dataList = new ArrayList<>();
            XjPlcDriver xjPlcDriver = XjPlcServer.plcDriverMap.get(deviceId);
            FourCarPlc fourCarPlc = xjPlcDriver.getDriverData().getFourCarPlcMap().get(deviceId);
            dataList.add(fourCarPlc);
            return Result.OK(dataList);
        }
        if(!SiemensPlcServer.plcDriverMap.isEmpty()){
            List<FourCarPlc> dataList = new ArrayList<>();
            SiemensPlcDriver siemensPlcDriver = SiemensPlcServer.plcDriverMap.get(deviceId);
            FourCarPlc fourCarPlc = siemensPlcDriver.getDriverData().getFourCarPlcMap().get(deviceId);
            dataList.add(fourCarPlc);
            return Result.OK(dataList);
        }
        return Result.OK(new ArrayList<>());
    }


//    @Override
//    public Result<?> changeDeviceByCode(String deviceCode) {
//        LambdaQueryWrapper<WcsDeviceInfo> deviceQuery = new LambdaQueryWrapper<>();
//        deviceQuery.eq(WcsDeviceInfo::getDeviceCode,deviceCode);
//        WcsDeviceInfo deviceInfo = this.getOne(deviceQuery);
////        if(WcsDeviceTypeEnums.FWV.getValue().equals(deviceInfo.getDeviceType())){
////            XjSxcProtocol sxcProtocol = XjPlcServer.sxcProtocolMap.get(deviceCode);
////            FourCarPlc fourCarPlc = sxcProtocol.xjPlcDriver.plcDataPacket.getFourCarReadPlcMap().get(deviceCode);
////            fourCarPlc.setAuto(fourCarPlc.isAuto()?false:true);
////        }else if(WcsDeviceTypeEnums.ELV.getValue().equals(deviceInfo.getDeviceType())){
////            XjHoistProtocol hoistProtocol = XjPlcServer.hoistProtocolMap.get(deviceCode);
////            HoistPlc hoistPlc = hoistProtocol.xjPlcDriver.plcDataPacket.getHoistReadPlcMap().get(deviceCode);
////            hoistPlc.setAuto(hoistPlc.isAuto()?false:true);
////        }
//        return Result.OK();
//    }


}
